package com.sky.config;


import com.sky.constant.ArgumentEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class RabbitMQConfiguration {

    public static final String EXCHANGE_NAME = "boot_routing_exchange";
    public static final String DLX_EXCHANGE_NAME = "dlx_boot_routing_exchange";
    public static final String QUEUE_NAME = "boot_queue";
    public static final String DLX_QUEUE_NAME = "dlx_boot_queue";
//
//    /**
//     * 延迟队列
//     */
    @Bean("bootQueue")
    public Queue createQueue() {
        Map<String, Object> args = new HashMap<>(3);
        // 设置消息存活时间
        long expirationTime = 10000;
        args.put(ArgumentEnum.X_MESSAGE_TTL_CONSTANT, expirationTime);
        // 设置死信交换机
        args.put(ArgumentEnum.X_DEAD_LETTER_EXCHANGE_CONSTANT, DLX_EXCHANGE_NAME);
        // 设置死信key
        args.put(ArgumentEnum.X_DEAD_LETTER_ROUTING_KEY_CONSTANT, "dlx_order");
        log.info("ttl_queue队列的存活时间为：{}ms",expirationTime);
        return new Queue("boot_queue", true, false, false, args);
    }
//
//    /**
//     * 创建交换机
//     */
//    @Bean
//    public DirectExchange createDirectExchange() {
//        return new DirectExchange(EXCHANGE_NAME, true, false);
//    }
//
//    /**
//     * 队列绑定交换机
//     */
//    @Bean
//    public Binding createDirectBinding() {
//        return BindingBuilder.bind(createQueue()).to(createDirectExchange()).with("order");
//    }



    @Bean("bootExchage")
    public Exchange bootExchange(){
        log.info("创建交换机");
        return ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(true).build();
    }
//
////    @Bean("dlxBootExchage")
////    public Exchange dlxBootExchange(){
////        log.info("创建交换机");
////        return ExchangeBuilder.directExchange(DLX_EXCHANGE_NAME).durable(true).build();
////    }
//
////    @Bean("dlxBootQueue")
////    public Queue dlxBootQueue(){
////        return QueueBuilder.durable(DLX_QUEUE_NAME).build();
////    }
//
//    @Bean("bootQueue")
//    public Queue bootQueue(){
//        return QueueBuilder.durable(QUEUE_NAME).build();
//    }
//
//
//
    @Bean
    public Binding bindQueueExchage(@Qualifier("bootQueue")Queue queue,@Qualifier("bootExchage") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("order").noargs();
    }

//    @Bean
//    public Binding dlxBindQueueExchage(@Qualifier("dlxBootQueue")Queue queue,@Qualifier("dlxBootExchage") Exchange exchange){
//        return BindingBuilder.bind(queue).to(exchange).with("dlx_order").noargs();
//    }

//    @Bean
//    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
//        RabbitAdmin rabbitAdmin=new RabbitAdmin(connectionFactory);
//        rabbitAdmin.setAutoStartup(true);
//        rabbitAdmin.declareExchange(dlxBootExchange());
//        rabbitAdmin.declareExchange(bootExchange());
//        rabbitAdmin.declareQueue(dlxBootQueue());
//        rabbitAdmin.declareQueue(bootQueue());
//        return rabbitAdmin;
//    }



}
